package com.samoyed.credit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.samoyed.credit.Modle.ListDataModle;
import com.samoyed.credit.adapter.MyAdapter;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private RecyclerView mRecylerView; //中间列表展示
    private ImageView btnManualAccounting; //手动记账按钮
    private ImageView btn_AiAccounting; //AI记账按钮
    private MyAdapter myAdapter;
    private ArrayList<ListDataModle> list;
    private Spinner spinner;
    private TextView tv_income_sum;
    private TextView tv_pay_sum;

    private double totalExpenses = 0; //总支出
    private double totalRevenue = 0; //总收入

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImmersionBar.with(this).transparentStatusBar().fullScreen(true).init();
        mRecylerView = findViewById(R.id.rec_list);
        btnManualAccounting = findViewById(R.id.btn_manual_accounting);
        btn_AiAccounting = findViewById(R.id.btn_ai_accounting);
        spinner = findViewById(R.id.spinner);
        tv_income_sum = findViewById(R.id.tv_income_sum);
        tv_pay_sum = findViewById(R.id.tv_pay_sum);

        btnManualAccounting.setOnClickListener(this);
        btn_AiAccounting.setOnClickListener(this);
//        initData();

        MMKV mmkv = MMKV.defaultMMKV();
        String listStr = mmkv.decodeString("list");
        list = new Gson().fromJson(listStr, new TypeToken<List<ListDataModle>>() {
        }.getType());
        if (list == null || list.size() == 0) {
            initData();
        }
        initSpinner();
        //线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecylerView.setLayoutManager(linearLayoutManager);

        myAdapter = new MyAdapter(list, this);
        mRecylerView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();

        myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(MainActivity.this, "你点击的是:" + position, Toast.LENGTH_SHORT).show();
            }
        });

        tv_income_sum.setText(totalRevenue + "");
        tv_pay_sum.setText(totalExpenses + "");

    }

    private void initData() {
        list = DataStr.INSTANCE.getList();
        Gson gson = new Gson();
        String listJson = gson.toJson(list);
        MMKV mmkv = MMKV.defaultMMKV();
        mmkv.encode("list", listJson);

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getType() == 1) {
                //本笔订单类型 1代表收入 2代表指出
                if (list.get(i).getOrdertype() == 1) {
                    totalRevenue += Double.parseDouble(list.get(i).getAmount());
                } else {
                    totalExpenses += Double.parseDouble(list.get(i).getAmount());
                }
            }
        }
    }

    @SuppressLint("ResourceType")
    private void initSpinner() {
        // 建立数据源
        String[] mItems = {"全年", "11月", "10月", "9月", "8月", "7月"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                MMKV mmkv = MMKV.defaultMMKV();
                String listStr = mmkv.decodeString("list");
                list = new Gson().fromJson(listStr, new TypeToken<List<ListDataModle>>() {
                }.getType());
                if (!"全年".equals(mItems[pos])) {
                    list = (ArrayList<ListDataModle>) DataStr.INSTANCE.getMonthList(list, mItems[pos]);
                }
                totalRevenue = 0;
                totalExpenses = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getType() == 1) {
                        //本笔订单类型 1代表收入 2代表指出
                        if (list.get(i).getOrdertype() == 1) {
                            totalRevenue += Double.parseDouble(list.get(i).getAmount());
                        } else {
                            totalExpenses += Double.parseDouble(list.get(i).getAmount());
                        }
                    }
                }
                tv_income_sum.setText(totalRevenue + "");
                tv_pay_sum.setText(totalExpenses + "");
                myAdapter.setList(list);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_ai_accounting:
                startActivity(new Intent(this, ImageTextAnalyseActivity.class));
                break;

            case R.id.btn_manual_accounting:
                startActivityForResult(new Intent(this, ManualAccountActivity.class), 0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            ListDataModle listDataModle = (ListDataModle) data.getSerializableExtra("mode");
            int number = DataStr.INSTANCE.isHasDate(list, listDataModle.getTime());
            if (number == 0) {
                list.add(0, listDataModle);
                ListDataModle listDataModle1 = (ListDataModle) listDataModle.clone();
                listDataModle1.setType(1);
                list.add(0, listDataModle1);
            } else {
                int index=DataStr.INSTANCE.getIndex(list,listDataModle.getTime());
                list.get(index).setAmount((Double.parseDouble(list.get(index).getAmount()) + Double.parseDouble(listDataModle.getAmount())) + "");
                list.add(index+1, listDataModle);
            }
            list = DataStr.INSTANCE.getListSort(list);
            Gson gson = new Gson();
            String listJson = gson.toJson(list);
            MMKV mmkv = MMKV.defaultMMKV();
            mmkv.encode("list", listJson);
            totalRevenue = 0;
            totalExpenses = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getType() == 1) {
                    //本笔订单类型 1代表收入 2代表指出
                    if (list.get(i).getOrdertype() == 1) {
                        totalRevenue += Double.parseDouble(list.get(i).getAmount());
                    } else {
                        totalExpenses += Double.parseDouble(list.get(i).getAmount());
                    }
                }
            }
            tv_income_sum.setText(totalRevenue + "");
            tv_pay_sum.setText(totalExpenses + "");
            myAdapter.notifyDataSetChanged();
        }
    }
}